﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Util;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;

namespace CourseSystem.Common.Helpers
{
    public class ScoreHelper
    {
        private static Dictionary<int, int> _questionStateDic;
        private static Dictionary<int, int> _postStateDic;

        static ScoreHelper()
        {
            Reload();
        }

        public static void Reload()
        {
            var info = CourseSettingBLL.Get();
            _questionStateDic = new Dictionary<int, int>();
            var spArray = info.QuestionState.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in spArray)
            {
                var itemArray = item.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (itemArray.Length != 2)
                    continue;
                _questionStateDic.Add(itemArray[0].CheckInt(0), itemArray[1].CheckInt(0));
            }
            _postStateDic = new Dictionary<int, int>();
            spArray = info.PostState.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in spArray)
            {
                var itemArray = item.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (itemArray.Length != 2)
                    continue;
                _postStateDic.Add(itemArray[0].CheckInt(0), itemArray[1].CheckInt(0));
            }
        }

        /// <summary>
        /// 章节练习+作业总成绩/（章节习题集数+作业次数）*%+提问情况*%+讨论情况*%+考试成绩*%。
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static int GetTotalScore(StudentInfo info)
        {
            var settingInfo = CourseSettingBLL.Get();
            var postCount = PostsBLL.GetPostCount(info.Id);
            var questionCount = QuestionBLL.GetAskCount(info.Id);
            var postScore = 0;
            for (var i = _postStateDic.Count - 1; i >= 0; i--)
            {
                var item = _postStateDic.ElementAt(i);
                if (postCount >= item.Key)
                {
                    postScore = item.Value;
                    break;
                }
            }
            var questionScore = 0;
            for (var i = _questionStateDic.Count - 1; i >= 0; i--)
            {
                var item = _questionStateDic.ElementAt(i);
                if (questionCount >= item.Key)
                {
                    questionScore = item.Value;
                    break;
                }
            }

            var examScore = ExamSummaryBLL.GetExamScore(info.Id);
            var exerciseScore = ExamSummaryBLL.GetSumScore(info.Id);
            var exerciseCount = ExamSummaryBLL.GetTypeCount(0) + ExamSummaryBLL.GetTypeCount(1);

            return (int)((float)exerciseScore.Division(exerciseCount) * settingInfo.HomeworkPersan / 100 +
                   (float)questionScore * settingInfo.QuestionPersan / 100
                   + (float)postScore * settingInfo.PostPersan / 100 + (float)examScore * settingInfo.ExamPersan / 100);
        }
    }
}
